package com.xydtech.ecard.ecard.controller;

import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.EcardApiAccessRecordList;
import com.xydtech.ecard.ecard.domain.EcardApiVisitRecord;
import com.xydtech.ecard.ecard.service.IEcardAccessRecordListService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 电子名片浏览记录明细Controller
 *
 * @author Daniel
 * @date 2021-09-14
 */
@Controller
@Api(tags = "电子名片-电子名片-浏览纪录")
@RequestMapping("/ecardSaasApi/ecard/access")
public class EcardAccessRecordListController extends EcardApiBaseController {
    @Autowired
    private IEcardAccessRecordListService ecardAccessRecordListService;

    /**
     * 新增电子名片访问记录
     * @param custmanagerRegistId
     * @return
     */
    @PassToken
    @ApiOperation(value = "电子名片-电子名片-新增电子名片访问记录", notes = "电子名片-电子名片-新增电子名片访问记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "custmanagerRegistId", value = "电子名片员工微信ID", dataType = "String", dataTypeClass = String.class, paramType = "query", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @PostMapping("/addEcardAccessRecordList")
    @ResponseBody
    public ResponseModel addEcardAccessRecordList(String custmanagerRegistId) {

        if (StringUtils.isEmpty(custmanagerRegistId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }

        EcardApiAccessRecordList ecardAccessRecordList = new EcardApiAccessRecordList();
        //当前登录人信息
        String currentUserId = getCurrentUserId();
        ecardAccessRecordList.setRegistId(currentUserId);
        //员工编号
        ecardAccessRecordList.setCustmanagerRegistId(custmanagerRegistId);

        int saveCount = ecardAccessRecordListService.insertEcardAccessRecordList(ecardAccessRecordList);
        return saveCount > 0 ? ResponseModel.ok() : ResponseModel.error("插入电子名片访问记录失败！");
    }

    /**
     * 电子名片-电子名片-获取电子名片访问的汇总数据
     * @return
     */
    @ApiOperation(value = "电子名片-电子名片-获取电子名片访问的汇总数据", notes = "电子名片-电子名片-获取电子名片访问的汇总数据")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @GetMapping("/getEcardAccessRecordTotalData")
    @ResponseBody
    public ResponseModel getEcardAccessRecordTotalData() {

        //获取当前登录人
        String currentUserId = getCurrentUserId();

        Map<String, Object> map = ecardAccessRecordListService.getEcardAccessRecordTotalData(currentUserId);

        return ResponseModel.ok(map);
    }

    /**
     * 电子名片-电子名片-获取电子名片的访问记录
     * @return
     */
    @ApiOperation(value = "电子名片-电子名片-获取电子名片的访问记录", notes = "电子名片-电子名片-获取电子名片的访问记录")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @GetMapping("/getEcardAccessRecordList")
    @ResponseBody
    public ResponseModel getEcardAccessRecordList() {

        //当前登录人信息
        String currentUserId = getCurrentUserId();
        startPage();
        List<EcardApiVisitRecord> apiVisitRecordList = ecardAccessRecordListService.getEcardAccessRecordList(currentUserId);
        return getTableResponse(apiVisitRecordList);
    }
}
